package cn.lwalan.commonUtils;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Random;
import org.apache.commons.lang3.StringUtils;

public class StringUtil {
	
	private static final String RANDOM_NUM = "0123456789";
	private static final String RANDOM_ABC_UP = "QWERTYUIOPASDFGHJKLMNBVCXZ";
	private static final String RANDOM_ABC_LOW = "qwertyuiopasdfghjklmnbvcxz"; 
	private static final String RANDOM_ABC_LOW_UP = "qwertyuiopasdfghjklmnbvcxzQWERTYUIOPASDFGHJKLMNBVCXZ"; 
	private static final String RANDOM_ABC_UP_NUM = "QWERTYUIOPASDFGHJKLMNBVCXZ0123456789";
	private static final String RANDOM_ABC_LOW_NUM = "qwertyuiopasdfghjklmnbvcxz0123456789";
	private static final String RANDOM_ABC_LOW_UP_NUM = "qwertyuiopasdfghjklmnbvcxzQWERTYUIOPASDFGHJKLMNBVCXZ0123456789";
	
	/**
	 * 获取后sublength位值
	 */
	public static String subLastStr(String sourceStr,int sublength){
		String newStr = null;
		if(StringUtils.isEmpty(sourceStr)){
			return null;
		}
		int sourceLength = sourceStr.length();
		if(sourceLength <= sublength){
			return sourceStr;
		}
		String tmpStr = sourceStr.substring(0, sourceLength-sublength);
		newStr = sourceStr.substring(tmpStr.length(),sourceLength);
		
		return newStr;
	}
	
	/**
	 * 获取序列号23位
	 * @return
	 */
	public static String getSerialNo() {			
		String orderCode = DateUtil.getFormatNowDate("yyyyMMddHHmmssSSS");
		Random r = new Random(new Date().getTime());
		int k = r.nextInt(900000) + 1000;
		orderCode = orderCode+k;
		return orderCode;
	}
	
	/**
	 * 将时间字符串截取为yyyy-MM-dd HH:mm:ss
	 * @param times
	 * @return
	 */
	public static String getTimeStr(String times){
		if(StringUtils.isEmpty(times)){
			return null;
		}
		String year = times.substring(0,4);
		String month = times.substring(4,6);
		String day = times.substring(6,8);
		String hour = times.substring(8,10);
		String minu = times.substring(10,12);
		String s = times.substring(12,14);
		return year + "-" + month+ "-" + day + " " +hour+":"+minu+":"+s;
	}

	/**
	 * 获取某个区间类的随机值
	 * randomStr "XX|XX" 最小值和最大值中间的随机数 竖线分割
	 * @param randomStr
	 * @return
	 */
	public static int getRandomValue(String randomStr) {
		
		String ranStr[] = randomStr.split("\\|");
		int minVal = Integer.parseInt(ranStr[0]);
		int maxVal = Integer.parseInt(ranStr[1]);
		Random random=new Random();
		int top = random.nextInt(maxVal)%(maxVal-minVal+1) + minVal;
		return top;
	}
	

	/**
	 * 获取N位随机数
	 * @param length
	 * @return
	 */
	public static long getXRandom(int length){
		if(length<1){  
			length = 1;    
	    }  
	    return (long)(Math.random()*9*Math.pow(10,length-1)) + (long)Math.pow(10,length-1);  
	}
	
	/**
	 * 
	 * 获取N位随机数  
	 * @param length 长度
	 * @param type 	0 纯数字 
	 * 				1 纯小写字母 
	 * 				2 纯大写字母 
	 * 				3 纯大小写字母 
	 * 				4 小写字母+数字
	 * 				5 大写字母+数字
	 * 				6 小写+大写+数字   	
	 * 				默认为纯数字
	 * @return
	 */
	public static String getRandom(int length,int type){
		if(length<1){  
			length = 1;    
	    }
		String str = "";
		Random random=new Random();
		switch (type) {
			case 0://0 纯数字 
				str = RANDOM_NUM;
				break;
			case 1://1 纯小写字母 
				str = RANDOM_ABC_LOW;
				break;
			case 2://2 纯大写字母 
				str = RANDOM_ABC_UP;
				break;
			case 3://3 纯大小写字母 
				str = RANDOM_ABC_LOW_UP;
				break;
			case 4://4 小写字母+数字
				str = RANDOM_ABC_LOW_NUM;
				break;
			case 5://5 大写字母+数字
				str = RANDOM_ABC_UP_NUM;
				break;
			case 6://6 小写+大写+数字  
				str = RANDOM_ABC_LOW_UP_NUM;
				break;
			default:
				str = RANDOM_NUM;
				break;
		}
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < length; i++) {
			int t = random.nextInt(str.length());
			sb.append(str.charAt(t));
		}
	    return sb.toString();  
	}
	
	/**
	 * 隐藏手机号中间四位
	 * @return
	 */
	public static String  hidMidMobile(String mobile){
		
		return mobile.replaceAll("(\\d{3})\\d{4}(\\d{4})","$1****$2");
	}
	
	/**
	 * 显示前N位后N位，隐藏中间N位
	 * @return
	 */
	public static String hidMidStr(String str,int beforeNum,int midNum,int afterNum){
		if((beforeNum+midNum+afterNum)> str.length()){
			return null;
		}
		
		return str.replaceAll("(\\d{"+beforeNum+"})\\d{"+midNum+"}(\\d{"+afterNum+"})","$1****$2");
	}
	
	/**
	 * 
	 * @param text		目标字符串
	 * @param length	截取长度
	 * @param encode	采用的编码方式
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public static String substring(String text, int length, String encode)
	throws UnsupportedEncodingException {
		if (text == null) {
			return null;
		}
		StringBuilder sb = new StringBuilder();
		int currentLength = 0;
		for (char c : text.toCharArray()) {
			currentLength += String.valueOf(c).getBytes(encode).length;
			if (currentLength <= length) {
			sb.append(c);
			}else{
				break;
			}
		}
		return sb.toString();
	}
	
	public static void main(String[] args) throws UnsupportedEncodingException {
		String tt = "你顶替顶替顶替加村夺顶替顶替顶替村顶替顶替";
		String tt2 = "你顶sdfsfsersdfsdfwerwerwerwr";
		System.out.println(substring(tt, 24,"UTF-8"));
		System.out.println(substring(tt2, 24,"UTF-8"));
	}
	
}
